_ = require 'underscore' 

# {findMerchant} = require '../merchant/merchant_serv'
{validate} = require '../utils/valid_utils'
{saveFile, removeFile, queryFile, findFile} = require './file_serv'
{saveValid, removeValid, queryValid, querySomeValid, findByIdValid, updateValid} = require './file_valid' 

# 保存文件上传
module.exports.save = (req, res, cbf) ->
  # 保存参数
  params =
    file_upload: req.body?.file_upload 

  # 校验数据
  validResult = validate params, saveValid?.rules, saveValid?.errorTips, true
  if 'success' isnt validResult?.status
    return cbf {msg: validResult}, null

  # 附加保存参数
  params.file_upload = {} if not params?.file_upload

  # 从session中读取商家ID、商家名称
  company_id = req.session?.user_back?.company_id or ''
  merchant_name = req.session?.user_back?.merchant_name or ''

  if '' isnt company_id
    # 商家ID、商家名称，附加到首页模块存储对象中
    params.file_upload?.company_id = company_id
    params.file_upload?.merchant_name = merchant_name

  # 保存文件上传
  return saveFile params?.file_upload, cbf 

# 删除文件上传
module.exports.remove = (req, res, cbf) ->
  # 删除条件
  params =
    file_upload_ids: req.body?.file_upload_ids 

  # 校验数据
  validResult = validate params, removeValid?.rules, removeValid?.errorTips
  if 'success' isnt validResult?.status
    return cbf {msg: validResult}, null 

  # 删除文件上传
  return removeFile {_id: {'$in': params?.file_upload_ids}}, cbf 

# 查询文件上传 - 分页查询
module.exports.query = (req, res, cbf) ->
  # 查询条件
  params = req?.body
  # 转换类型
  params.pageNum = (parseInt params?.pageNum, 10) or 1
  params.pageSize = (parseInt params?.pageSize, 10) or 10

  # 校验数据
  validResult = validate params, queryValid?.rules, queryValid?.errorTips
  if 'success' isnt validResult?.status
    return cbf {msg: validResult}, null 

  params.query = {} if not params?.query
  company_id = req.session?.user_back?.company_id or ''
  params.query?.company_id = company_id

  # 查询文件上传
  return queryFile params, cbf

# 查询多个文件上传 - 不分页
module.exports.querySome = (req, res, cbf) ->
  # 查询条件
  params =
    file_upload_ids: req.body?.file_upload_ids 

  # 校验数据
  validResult = validate params, querySomeValid?.rules, querySomeValid?.errorTips
  if 'success' isnt validResult?.status
    return cbf {msg: validResult}, null 

  # 查询文件上传
  return findFile {_id: {'$in': params?.file_upload_ids}}, cbf 

# 查询单个文件上传
module.exports.findById = (req, res, cbf) ->
  # 文件上传_id
  file_upload_id = req.body?.file_upload_id 

  # 查询条件
  params =
    file_upload_id: file_upload_id 

  # 校验数据
  validResult = validate params, findByIdValid?.rules, findByIdValid?.errorTips
  if 'success' isnt validResult?.status
    return cbf {msg: validResult}, null 

  # 查询文件上传
  return findFile {_id: params?.file_upload_id}, cbf 

# 修改文件上传
module.exports.update = (req, res, cbf) ->
  # 保存参数
  params =
    file_upload: req.body?.file_upload 

  # 校验数据
  validResult = validate params, updateValid?.rules, updateValid?.errorTips, true
  if 'success' isnt validResult?.status
    return cbf {msg: validResult}, null 

  # 修改字段名
  params.file_upload._id = params.file_upload.file_upload_id
  delete params.file_upload.file_upload_id 

  # 修改文件上传
  return saveFile params?.file_upload, cbf